package com.jweb.util;

import java.lang.management.ManagementFactory;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class UUIDUtil {
	private final static String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	private static long curLocalIP = 0L;
	private static long curPID = 0L;

	public static String getUUID36() {
		return UUID.randomUUID().toString();
	}

	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 获取UUID
	 * 
	 * @param len
	 *            长度
	 * @param jz
	 *            进制[10~62]
	 */
	public static String getUUID(int len, int jz) {
		if (jz < 10 && jz > 62)
			jz = 32;
		String cu = "";
		if (len <= 12) {
			cu = parseStrJZ(System.currentTimeMillis(), jz) + getCharStr(4, jz);
		} else if (len > 12) {
			if (curLocalIP == 0) {
				try {
					curLocalIP = Long.parseLong(HttpServletRequestUtil.getLocalRealIp().replaceAll("\\.", ""));
				} catch (Exception e) {
					e.printStackTrace();
					curLocalIP = 255255255255L;
				}
				System.out.println("【curLocalIP=" + curLocalIP + "】");
			}
			if (curPID == 0) {
				String cPname = ManagementFactory.getRuntimeMXBean().getName();
				curPID = Long.parseLong(cPname.substring(0, cPname.indexOf("@")));
				System.out.println("【curPID=" + curPID + "】");
			}
			long cTid = Thread.currentThread().getId();
			String cos = parseStrJZ(curLocalIP + curPID + cTid, jz);
			if (len < 18) {
				cu = parseStrJZ(System.currentTimeMillis(), jz) + cos.substring(cos.length() - 4) + getCharStr(6, jz);
			} else {
				cu = parseStrJZ(System.currentTimeMillis(), jz) + cos;
				cu = cu + getCharStr(len - cu.length(), jz);
			}
		}
		if (cu.length() < len)
			cu += getCharStr(len - cu.length(), jz);
		return cu.substring(cu.length() - len);
	}

	public static void main2(String[] args) {
		long x = System.currentTimeMillis();
		// System.out.println(x);
		System.out.println(parseStrJZ(238328, 62));
		// System.out.println(parseStrJZ(x, 62));
		// System.out.println(parseStrJZ(x, 16));
		// System.out.println(parseLong10(parseStrJZ(x, 36), 36));
		// System.out.println(parseLong10(parseStrJZ(x, 62), 62));
		// System.out.println(parseLong10(parseStrJZ(x, 16), 16));
		System.out.println(parseLong10("ZZZ", 62));
	}

	public static void main(String[] args) {
		long x = System.currentTimeMillis();
		Set<String> set = new HashSet<String>();
		for (int i = 0; i < 50000; i++) {
			set.add(getUUID(16, 62));
		}
		long en = System.currentTimeMillis() - x;
		int y = 0;
		for (String string : set) {
			y++;
			if (y > 10)
				break;
			System.out.println(string);
		}
		System.out.println(en);
		System.out.println(set.size());
	}

	public static String getCharStr(int len, int jz) {
		String a = "";
		for (int i = 0; i < len; i++)
			a += chars.charAt((int) Math.round(Math.random() * (jz - 1)));
		return a;
	}

	public static String parseStrJZ(long num, int jz) {
		String str = "";
		if (num == 0) {
			return "";
		} else {
			str = parseStrJZ(num / jz, jz);
			return str + chars.charAt((int) (num % jz));
		}
	}

	public static long parseLong10(String str, int jz) {
		long result = 0;
		int len = str.length();
		for (int i = len; i > 0; i--) {
			result += chars.indexOf(str.charAt(i - 1)) * ((long) (Math.pow(jz, len - i)));
		}
		return result;
	}

}